package com.gzhu.knowledgeAdmin.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gzhu.knowledgeAdmin.common.utils.UserInfoUtils;
import com.gzhu.knowledgeAdmin.dao.TRwTMapper;
import com.gzhu.knowledgeAdmin.entity.TRwT;
import com.gzhu.knowledgeAdmin.entity.User;
import com.gzhu.knowledgeAdmin.repository.TRwTRepository;
import com.gzhu.knowledgeAdmin.service.TRwTService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class TRwTServiceImpl extends ServiceImpl<TRwTMapper, TRwT> implements TRwTService {

    @Resource
    private TRwTRepository tRwTRepository;

    @Resource
    private TRwTMapper tRwTMapper;

    @Override
    public TRwT findById(Long fRwid) {
        TRwT tRwT = tRwTMapper.selectById(fRwid);
        Integer fRwlx = tRwT.getFRwlx();
        if(fRwlx == 3 || fRwlx == 4 || fRwlx == 5 ){
            tRwT.setFRwlx(1);
        }
        return tRwT;
    }

    @Override
    public List<TRwT> selectBySearch(TRwT tRwT) {
        return tRwTMapper.selectBySearch(tRwT);
    }

    @Override
    public Long selectMaxNum() {
        return tRwTMapper.selectMaxNum();
    }

    @Override
    public TRwT saveTask(TRwT tRwT) {

        //设置状态
        if(tRwT.getFRwqdfs() == 0)
        {
            tRwT.setFRwzt(1);
            //tRwT.setFRwzt(2);
            LocalDateTime now = LocalDateTime.now();
            tRwT.setFRwksrq(now);
            DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm");
            String formattedTime = now.format(timeFormatter);
            tRwT.setFDsqdsj(formattedTime);

        }
        else if(tRwT.getFRwqdfs() == 1)
        {
            tRwT.setFRwzt(1);
        }

        //设置类型
        if(tRwT.getFRwlx()==1&&tRwT.getFJtzjlx()==0)
        {
            tRwT.setFRwlx(3);
        }
        if(tRwT.getFRwlx()==1&&tRwT.getFJtzjlx()==1)
        {
            tRwT.setFRwlx(4);
        }
        if(tRwT.getFRwlx()==1&&tRwT.getFJtzjlx()==2)
        {
            tRwT.setFRwlx(5);
        }

//        //设置任务id
//        if(tRwT.getFRwid()==null)
//        {
//            Long taskCount = selectMaxNum();
//            Long taskNumber = taskCount + 1;
//            tRwT.setFRwid(taskNumber);
//        }
        User currentUser = UserInfoUtils.getCurrentUser();
        tRwT.setFGsid(currentUser.getGsid());
        Date date = new Date();
        tRwT.setFRwjlsj(date);
        return tRwTRepository.save(tRwT);
    }

    @Override
    public Integer updateTask(TRwT tRwT) {

        //设置状态
        if(tRwT.getFRwqdfs() == 0)
        {
            tRwT.setFRwzt(2);
        }
        else if(tRwT.getFRwqdfs() == 1)
        {
            tRwT.setFRwzt(1);
        }

        //设置类型
        if(tRwT.getFRwlx()==1&&tRwT.getFJtzjlx()==0)
        {
            tRwT.setFRwlx(3);
        }
        if(tRwT.getFRwlx()==1&&tRwT.getFJtzjlx()==1)
        {
            tRwT.setFRwlx(4);
        }
        if(tRwT.getFRwlx()==1&&tRwT.getFJtzjlx()==2)
        {
            tRwT.setFRwlx(5);
        }

        return tRwTMapper.updateTask(tRwT);
    }


    @Override
    public List<String> findOwners() {
        return tRwTMapper.findOwners();
    }

    @Override
    public Integer startTasks(List<Long> rwids) {
        return tRwTMapper.startTasks(rwids);
    }

    @Override
    public Integer stopTasks(List<Long> rwids) {
        return tRwTMapper.stopTasks(rwids);
    }

    @Override
    public Integer recallTasks(List<Long> rwids) {
        return tRwTMapper.recallTasks(rwids);
    }

    @Override
    public List<TRwT> getAllTasks() {
        return list();
    }

    @Override
    public List<Map<String, Object>> getAllTasksWithCompanyName() {
        return tRwTMapper.selectAllTasksWithCompanyName();
    }
}
